#include "cdDispose.h"


int cdDispose(char * str ,int sockfd,Stack * s){

    Stack ass_s;
    stackInit(&ass_s);
    for(int i = s->size-1  ; i >= 0 ; --i){    //深拷贝    记录原始路径
        char * ass_str = (char *)calloc(strlen(s->str[i])+1,sizeof(char));
        strncpy(ass_str,s->str[i],strlen(s->str[i]));
        push(&ass_s,ass_str);
    }
    
    int nn = 0;
    while(nn < strlen(str) &&str[nn]!=' '){
        ++nn;
    }
    while(nn < strlen(str) &&  str[nn]==' '){
        ++nn;
    }
    if(nn >= strlen(str)){
        return 0;
    }
    if(str[nn] == '/'){                    
        destroyStack(s);
        char * aaaa = (char *)calloc(2,sizeof(char));
        aaaa[0] = '/';
        push(s,aaaa);
        if(nn+1==strlen(str)){
            return 0;
        }
        for(int i = nn ; i < strlen(str) ; ++i){
            str[i] = str[i+1];
        }
    }
    /* printf("str:%s\n",str); */

    char * command = strtok(str," ");    //记录cd命令
        
    

    while(!isEmpty(s)){                   //检查路径是否合理，并得出屏幕应该输出的合理路径。
        char * p = strtok(NULL,"/");
        if(p == NULL){
            break;
        }
        if(strcmp(p,"~")==0){
            destroyStack(s);
            char * bbbb = (char *)calloc(5,sizeof(char));
            bbbb[0] = '~';
            push(s,bbbb);
            continue;
        }
        if(strcmp(p,".")==0){
            continue;
        }
        if(strcmp(p,"..")== 0){
            free(top(s));
            pop(s);
            continue;
        }
        char * ass_str = (char*)calloc(strlen(p)+1,sizeof(char));
        strncpy(ass_str,p,strlen(p));
        push(s,ass_str);
    }

    if(isEmpty(s)){        //恢复路径栈内容
        printf("路径输入错误！\n");
        while(!isEmpty(&ass_s)){
            push(s,top(&ass_s));
            pop(&ass_s);
        }
        return -1;
    }

    if(s->size == 1){
        return 0;
    }
    //栈不为空，说明路径合理。
    char path[1024] = {0};      //遍历s，得出当前服务器路径（三四期中就是真实路径）  /dsf/dsf/fsd
    for(int i = 1 ; i < s->size ; ++i){
        char xiegang[] = "/";
        char sstr[128]={0};
        memcpy(sstr,s->str[i],strlen(s->str[i]));
        strcat(path,xiegang);
        strcat(path,sstr);
    }                  
    /* printf("path:%s\n",path); */   

    Train train; //向后台发送cd信号
    train.length = strlen(command);
    memcpy(train.data,command,train.length);
    send(sockfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);
    
    train.length = strlen(path);   //发送目录文件路径            
    memcpy(train.data,path,train.length);
    send(sockfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);
    
    int epollfd = epoll_create(1);       //等待接收“是否有”信号
    struct epoll_event event;
    event.events = EPOLLIN;
    event.data.fd = sockfd;
    epoll_ctl(epollfd,EPOLL_CTL_ADD,sockfd,&event);
    struct epoll_event readyset[5];
    epoll_wait(epollfd,readyset,5,-1);
    
    char has;
    recv(readyset[0].data.fd,&has,1,MSG_WAITALL);
    /* printf("%c\n",has); */

    if(has == '0'){
        printf("目录或文件不存在\n");
        destroyStack(s);          //清空路径栈
        while(!isEmpty(&ass_s)){  //恢复路径栈
            push(s,top(&ass_s));
            pop(&ass_s);
        }
        destroyStack(&ass_s); 
        return -1;
    }

    return 0;
}



